Forward declaration

A Forward declaration is a type of declaration which enables the compiler to resolve references from different parts of a program. The purpose being to allow a programmer to refer to objects that the compiler may not yet have met but that will be defined later in the compilation process.

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, or a function) for which the programmer has not yet given a complete definition. It is required for a compiler to know the type (size) of an identifier, but not a particular value it holds (in case of variables).

void printThisInteger(int);

In C/C++, the line above represents forward declaration of a function and is the function's prototype. After processing this declaration, the compiler would allow the programmer to refer to the entity printThisInteger in the rest of the program. Definition for a function must be provided somewhere (same file or other, where it would be responsibility of the linker to correctly match references to particular function in one or several object files with its definition, which must be unique, in another):

void printThisInteger(int x) {
   printf("%d\n", x);
}

Variables may have only forward declaration and lack definition. During compilation time these are initialized by language specific rules (to undefined values, 0, NULL pointers, ...). Variables, which are defined in other source/object file, must have a forward declaration specified with a keyword extern:

int foo; //foo might be defined somewhere in this file
extern int bar; //bar must be defined in some other file

In Pascal and other Wirth programming languages, it is a general rule that all entities must be declared before use. In C, the same general rule applies, but with an exception for undeclared functions and incomplete types. Thus, in C it is possible (although unwise) to implement a pair of mutually recursive functions thus:

int first(int x) {
   if (x == 0)
      return 1;
 
   return second(x-1); //forward reference to second
}
 
int second(int x) {
   if (x == 0)
      return 0;
 
   return first(x-1);
}

In Pascal, the same implementation requires a forward declaration of second to precede its use in first. Without the forward declaration, the compiler will produce an error message indicating that the identifier second has been used without being declared.

Forward reference

The term forward reference is sometimes used as a synonym of forward declaration.[1] However, more often it is taken to refer to the actual use of an entity before any declaration; that is, the first reference to second in the code above is a forward reference.[2][3] Thus, we may say that because forward declarations are mandatory in Pascal, forward references are prohibited.

An example of (valid) forward reference in C++:

class C {
public:
   void mutator(int x) { myValue = x; }
   int accessor() { return myValue; }
private:
   int myValue;
};

In this example, there are two references to myValue before it is declared. C++ generally prohibits forward references, but they are allowed in the special case of class members. Since the member function accessor cannot be compiled until the compiler knows the type of the member variable myValue, it is the compiler's responsibility to remember the definition of accessor until it sees myValue's declaration.

Permitting forward references can greatly increase the complexity and memory requirements of a compiler, and generally prevents the compiler from being implemented in one pass.

References

  1. ^ MSDN: Converting to a Forward-Reference Class Type
  2. ^ http://pages.cs.wisc.edu/~fischer/cs536.s07/lectures/Lecture25.4up.pdf
  3. ^ Thinking in C++: Inlines & the compiler